package com.xzx.masterslave.masterslave.aop;

import com.xzx.masterslave.masterslave.context.DataSouceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 切换数据源aop
 * @author admin
 */
@Aspect
@Component
@Slf4j

public class ChangeDataSourceAop {

    /**
     * 写注解切点
     */
    @Pointcut("@annotation(com.xzx.masterslave.masterslave.annotation.WriteAnnonation)")
    public void writeMethod(){

    }

    /**
     * 读注解切点
     */
    @Pointcut("@annotation(com.xzx.masterslave.masterslave.annotation.ReadAnnonation)")
    public void readMehod() {

    }

    //通知
    @Before("writeMethod()")
    public void beforeWritre(JoinPoint point){
        //执行切换数据源逻辑
        DataSouceContextHolder.write();
        log.info("ThreadLocalType ====>"+DataSouceContextHolder.getJdbcType());
        String className = point.getTarget().getClass().getName();
        String methodName =  point.getSignature().getName();
        log.info("className===>"+className);
        log.info("methodName===>"+methodName);
    }


    @Before("readMehod()")
    public void beforeRead(JoinPoint point){
        //执行切换数据源逻辑
        DataSouceContextHolder.read();
        log.info("ThreadLocalType ====>"+DataSouceContextHolder.getJdbcType());
        String className = point.getTarget().getClass().getName();
        String methodName =  point.getSignature().getName();
        log.info("className===>"+className);
        log.info("methodName===>"+methodName);
    }

}
